<?php
namespace SYSTEM;

class View {

    private $viewsPath = './application/views/';
    private $systemViewPath = './system/views/';
    private $data = array();
    
    private $url = null;
    public $lang = null;
    
    public function __construct() {
        $this->url = new \SYSTEM\HELPERS\Url();
        $this->lang = new \SYSTEM\Languages();
    }

    public function set($arrData_key, $val = null) {
        if(!$arrData_key) return;
        if(is_array($arrData_key) && count($arrData_key) && !$val) {
            foreach ($arrData_key as $k => $v) {
                $this->_set($k, $v);
            }
        } else {
            $this->_set($arrData_key, $val);
        }
    }
    
    private function _set($k, $v) {
        $this->data[$k] = $v;
    }

    public function get($key) {
        if(array_key_exists($key, $this->data)) {
            return $this->data[$key];
        }
        
        return "";
    }

    public function parse($viewName, $controllerData = array(), $isSystem = false) {
        $view = realpath(
            ($isSystem === true ? $this->systemViewPath : $this->viewsPath).$viewName.'.php'
        );
        if(!file_exists($view)) {
            die("View ".$viewName." doesnt exists in application/views/");
        }
        $this->set($controllerData);
        
        ob_start();
        require $view;
        $output = ob_get_contents();
        ob_end_clean();
        
        return $output;
    }
}